﻿<Controls:WorkBaseControl x:Class="SimpleSpreadsheet.Views.WorksheetView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:cal="clr-namespace:SimpleSpreadsheet.Framework"
             xmlns:Views="clr-namespace:SimpleSpreadsheet.Views"
             xmlns:local="clr-namespace:SimpleSpreadsheet"
             xmlns:Controls="clr-namespace:SimpleSpreadsheet.Controls" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Loaded="UserControl_Loaded">
    <Controls:WorkBaseControl.Resources>
        <ControlTemplate x:Key="cellTemplate" TargetType="Button">
            <Border Name="cellBorder"
                    BorderBrush="Gray"
                    BorderThickness="0.3"
                    Background="Transparent"
                    Padding="2">
                <Grid>
                    <ContentPresenter
                        Name="cellContent"
                        Content="{TemplateBinding Content}"
                        ContentTemplate="{TemplateBinding ContentTemplate}"
                        Margin="{TemplateBinding Padding}"
                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Grid>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsFocused" Value="True">
                    <Setter TargetName="cellBorder" Property="BorderBrush" Value="Black" />
                    <Setter TargetName="cellBorder" Property="BorderThickness" Value="3" />
                    <Setter TargetName="cellBorder" Property="Background" Value="Lavender" />
                    <Setter TargetName="cellBorder" Property="Padding" Value="0" />
                </Trigger>
                <Trigger Property="IsKeyboardFocused" Value="True">
                    <Setter TargetName="cellBorder" Property="BorderBrush" Value="Black" />
                    <Setter TargetName="cellBorder" Property="BorderThickness" Value="3" />
                    <Setter TargetName="cellBorder" Property="Background" Value="Lavender" />
                    <Setter TargetName="cellBorder" Property="Padding" Value="0" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Controls:WorkBaseControl.Resources>
    <Grid Focusable="False">
        <Grid.RowDefinitions>
            <RowDefinition Height="22"></RowDefinition>
            <RowDefinition Height="20"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="25"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <Rectangle Grid.Column="0" Grid.Row="1" Fill="Gainsboro"></Rectangle>

        <!--Expression edit area-->
        <Controls:CustomTextBox Grid.Row="0" Grid.ColumnSpan="2" EnterKeyPressed="CurrentCell_EnterKeyPressed" x:Name="CurrentCell" />

        <!--Row headers-->
        <ItemsControl Grid.Row="2" Grid.Column="0" x:Name="RowHeaders" Focusable="False">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Vertical"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border Name="cellBorder"
                        BorderBrush="Gray"
                        BorderThickness="1"
                        Background="Gainsboro">
                        <TextBlock
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center"
                                Height="20"
                                Text="{Binding}">
                        </TextBlock>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

        <!--Column headers-->
        <ItemsControl Grid.Row="1" Grid.Column="1" x:Name="ColumnHeaders" Focusable="False">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border Name="cellBorder"
                        BorderBrush="Gray"
                        BorderThickness="1"
                        Background="Gainsboro"
                        HorizontalAlignment="Center">
                        <TextBlock
                            HorizontalAlignment="Center"
                            Width="62"
                            Text="{Binding}">
                        </TextBlock>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

        <!--Worksheet-->
        <ItemsControl
            Height="Auto"
            Width="Auto"
            VirtualizingStackPanel.IsVirtualizing="True"
            VirtualizingStackPanel.VirtualizationMode="Standard"
            ScrollViewer.CanContentScroll="True"
            VerticalAlignment="Stretch"
            HorizontalAlignment="Stretch"
            Grid.Row="2"
            Grid.Column="1"
            x:Name="Cells"
            Focusable="False"
            KeyboardNavigation.DirectionalNavigation="Contained"
            KeyUp="Cells_KeyUp">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel Orientation="Vertical"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ItemsControl
                        VirtualizingStackPanel.IsVirtualizing="True"
                        VirtualizingStackPanel.VirtualizationMode="Standard"
                        ItemsSource="{Binding}"
                        Focusable="False">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <VirtualizingStackPanel Orientation="Horizontal"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Controls:CellControl
                                    ColumnNumber="{Binding Path=Column}"
                                    RowNumber="{Binding Path=Row}"
                                    HorizontalContentAlignment="Right"
                                    VerticalContentAlignment="Bottom"
                                    Template="{DynamicResource cellTemplate}"
                                    cal:Message.Attach="[Event Click] = [SelectCurrentCell($dataContext)]"
                                    Focusable="True"
                                    Width="64"
                                    Height="22"
                                    Content="{Binding Path=DisplayValue, Mode=OneWay}"
                                    GotFocus="Button_GotFocus">
                                </Controls:CellControl>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.Template>
                <ControlTemplate>
                    <Border
                        BorderThickness="{TemplateBinding Border.BorderThickness}"
                        Padding="{TemplateBinding Control.Padding}"
                        BorderBrush="{TemplateBinding Border.BorderBrush}"
                        Background="{TemplateBinding Panel.Background}"
                        SnapsToDevicePixels="True">
                        <ScrollViewer
                            HorizontalScrollBarVisibility="Disabled"
                            VerticalScrollBarVisibility="Disabled"
                            Padding="{TemplateBinding Control.Padding}"
                            Focusable="False">
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </ItemsControl.Template>
        </ItemsControl>
    </Grid>
</Controls:WorkBaseControl>
